package com.software.backend.searchmovie.service;

import com.software.backend.searchmovie.entity.Movie;
import com.software.backend.searchmovie.entity.Video;
import com.software.backend.searchmovie.service.repository.MovieRepository;
import com.software.backend.searchmovie.service.repository.VideoRepository;
import com.software.backend.searchmovie.utils.Constant;
import com.software.backend.searchmovie.utils.FFmpegUtil;
import com.software.backend.searchmovie.utils.QiniuCloudUtil;
import com.software.backend.searchmovie.utils.SearchMovieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @program: File_demo
 * @description:
 * @author: C@t
 * @create: 2022-11-10 09:24
 **/
@Service
public class MovieServiceImpl implements MovieService {
    @Autowired
    private MovieRepository movieRepository;
    @Autowired
    private VideoRepository videoRepository;
    @Autowired
    private HttpSession session;
    static FFmpegUtil fFmpegUtil = new FFmpegUtil();
    static QiniuCloudUtil qiniuCloudUtil = new QiniuCloudUtil();
    @Override
    public List<Movie> searchMovies(String searchStr) {

        List<Movie> movieList = new ArrayList<>();
        String path = this.getClass().getResource(Constant.MOVIE_DIR_PATH).getPath();
        System.out.println(path);
        try {
            List<String> fileNames = SearchMovieUtil.scanAllFileFromDir(path);
            for (String fileName : fileNames) {
                List<Video> videoList = SearchMovieUtil.scanFile(path + "\\" + fileName, searchStr);
//                System.out.println(fileName);
                String[] file = fileName.split(".txt");
                String movieName = file[0];
                if (videoList.size() > 0) {

                    if (videoList.size()>5) {
                        List<Video> newVideoList = new ArrayList<>();

//                        for (int i = 5; i < videoList.size(); i++) {
//                            videoList.remove(i);
//                        }
                        int n = videoList.size();
                        Random rand = new Random();
                        boolean[] bool = new boolean[n];
                        int randInt = 0;
                        for(int i = 0; i < 5 ; i++) {
                            do {
                                randInt = rand.nextInt(n);
                            }while(bool[randInt]);
                            bool[randInt] = true;
//                            System.out.println(randInt);
                            newVideoList.add(videoList.get(randInt));
                        }
                        videoList = newVideoList;
                    }
                    //通过电影名字返回Movie,添加到MovieList，然后赋值id给这批Video
                    Movie movie = searchMovieByName(movieName);
                    movieList.add(movie);
                    for (Video video : videoList) {
                        video.setMovieId(movie.getId());//这个位置写电影的Id
                    }
//                    System.out.println(fileName+":"+videoList.size());
                    session.setAttribute(movie.getId() + "movie", movie);
                    session.setAttribute(movie.getId() + "videoList", videoList);

                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return movieList;
    }

    @Override
    public Movie searchMovieByName(String MovieName) {
        System.out.println(MovieName);
        //动态查询
        Specification specification = new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                predicateList.add(criteriaBuilder.like(root.get("name"), MovieName));
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        List<Movie> all = movieRepository.findAll(specification);
        System.out.println(all.toString());
        return all.get(0);
    }

    @Override
    public List<Video> findVideoByMovieId(int movieId) {
        Specification<Video> specification = new Specification<Video>() {
            @Override
            public Predicate toPredicate(Root<Video> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                predicateList.add(criteriaBuilder.equal(root.get("movieId"), movieId));
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }

        };
        List<Video> videoList = movieRepository.findAll(specification);
        return videoList;
    }
}
